<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/6/25
 * Time: 14:08
 */

namespace app\admin\logic;


use app\admin\logic\code\ErrorCode;
use app\common\model\Ad;

class adLogic extends BaseLogic{
    public function ajaxList(){
        $list = Ad::select();
        $count = Ad::count();
        return ajaxArr(ErrorCode::$SUCCESS, 'success', $list, $count);
    }
    public function info($id){
        $info = Ad::get($id);
        if ($info){
            return ajaxArr(ErrorCode::$SUCCESS, 'success',$info);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '信息为空');
        }
    }
    public function handle($data){
        $act = $data['act'];
        unset($data['act']);
        $res = false;
        switch ($act){
            case 'add':
                $insert['title'] = $data['title'];
                $insert['url'] = $data['url'];
                $insert['cate_id'] = $data['cate_id'];
                $insert['status'] = $data['status']?$data['status']:0;
                $insert['ordid'] = $data['ordid'];
                $insert['thumb'] = $data['thumb'];
                $insert['resume'] = $data['resume'];
                $insert['createtime'] = time();
                $res = Ad::insert($insert);
                break;
            case 'edit':
                $update['title'] = $data['title'];
                $update['cate_id'] = $data['cate_id'];
                $update['url'] = $data['url'];
                $update['status'] = $data['status']?$data['status']:0;
                $update['ordid'] = $data['ordid'];
                $update['resume'] = $data['resume'];
                $update['thumb'] = $data['thumb'];
                $res = Ad::where('id','eq',$data['id'])->update($update);
                break;
            case 'del':
                $cateInfo = Ad::get($data['id']);
                $res = $cateInfo->delete();
                break;
            default:
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }
}